package light.head.framework.utils;

/**
 * @author 简航 创建日期 2009-08-05 15:05:13
 * 位移操作
 */
public class DisplacementUtils {
	/**
	 * 按字节反向生成一个整型变量数组中的所有整型变量
	 * 
	 * @param 要反向生成的整型变量数组
	 * @return 生成的整型数组
	 */
	public static int[] deserializeInts(int[] numList) {
		int[] newList = new int[numList.length];
		for (int i = 0; i < numList.length; i++)
			newList[i] = deserializeInt(numList[i]);
		return newList;
	}

	/**
	 * 按字节反向生成一个整型变量
	 * 
	 * @param 要反向生成的整型变量
	 * @return 生成后的整型变量
	 */
	public static int deserializeInt(int num) {
		byte[] buf = new byte[4];
		byte[] newbs = new byte[4];
		int2bytes(num, buf, 0);
		for (int i = 0; i < 4; i++) {
			newbs[i] = buf[4 - i - 1];
		}

		return bytes2int(newbs);
	}

	/**
	 * byte数组转int数组, 注: 最后一位需要移位, 例: 123456789, 前两位填满1234,5678, 最后一位为: 0009
	 * 
	 * @param 要转换的byte数组
	 * @param
	 * @return
	 * @author 简航 创建日期 2009-08-05 14:54:54
	 */
	public static int[] bytes2ints(byte[] bs, int listCount) {
		byte[] tempbs = null;
		int[] v = new int[listCount];
		for (int i = 0; i < listCount; i++) {
			tempbs = new byte[4];
			for (int j = 0; j < 4 && j < bs.length - (i * 4); j++) {
				tempbs[j] = bs[i * 4 + j];
			}
			v[i] = bytes2int(tempbs);

		}

		return v;
	}

	/**
	 * 把一个byte数组以4：1的方式填充到一个整型数组中, 如果byte数组不足4为就往右边移
	 * 
	 * @param byte数组
	 * @return 填充好的整型数组
	 */
	public static int[] bytes2ints(byte[] bs) {
		int i = bs.length % 4 == 0 ? bs.length / 4 : bs.length / 4 + 1;
		int[] result = new int[i];
		int length = bs.length;
		byte[] tempbs;
		for (int x = 0; x < i; x++) {
			tempbs = new byte[length - (x * 4) > 4 ? 4 : length - (x * 4)];
			for (int y = 0; y < 4 && length > (x * 4) + y; y++) {
				tempbs[y] = bs[x * 4 + y];
			}
			result[x] = myBytesToIntByC(tempbs);
		}
		return result;
	}

	/**
	 * byte数组转int数组, 并截取余掉的
	 * 
	 * @param byte数组
	 * @return 转换好的int数组
	 * @author 简航 创建日期 2009-08-06 23:30:36
	 */
	public static int[] bytes2ints_intercept(byte[] bs) {
		int[] ints = new int[bs.length / 4];
		byte[] temp = new byte[4];
		for (int i = 0, j = 0; i < bs.length && j < ints.length; i += 4, j++) {
			temp[0] = bs[i];
			temp[1] = bs[i + 1];
			temp[2] = bs[i + 2];
			temp[3] = bs[i + 3];

			ints[j] = bytesToInt(temp);
		}
		return ints;
	}

	/**
	 * byte转int, 最后不足4位的往后面移
	 * 
	 * @param bs
	 * @return
	 * @author 简航 创建日期 2009-08-05 15:06:51
	 */
	public static int myBytesToIntByC(byte[] bs) {
		int result1 = 0;

		byte[] newbs = new byte[4];
		if (bs.length == 4)
			newbs = bs;
		else {
			int i = 0, j = 0;
			for (; i < 4 - bs.length; i++, j++)
				newbs[i] = 0;
			for (; i < 4; i++) {
				newbs[i] = bs[i - j];
			}
		}

		int mask = 0xff;
		int temp = 0;
		for (int i = 0; i < 4; i++) {
			result1 <<= 8;
			temp = newbs[i] & mask;
			result1 |= temp;
		}
		return result1;
	}

	/**
	 * byte转整型
	 * 
	 * @param 要转换的byte
	 * @return 转换后的整型数据
	 */
	public static int unsignedByteToInt(byte b) {
		return (int) b & 0xFF;
	}

	/**
	 * 获取byte的16进制字符串显示形式
	 * 
	 * @param 要获取的byte
	 * @return 得到的16进制字符串显示形式
	 */
	public static String byteToHex(byte b) {
		int i = b & 0xFF;
		return Integer.toHexString(i);
	}

	/**
	 * 字符串转短整型数组yBytesToIntByC(byte[] bs) {
	 * 
	 * @param 目标字符串
	 * @return 转换后的短整型数组
	 */
	public static short[] str2sht(String str) {
		short[] shtList = new short[str.length()];
		for (int i = 0; i < str.length(); i++) {
			shtList[i] = (short) str.charAt(i);
		}
		return shtList;
	}

	/**
	 * 在指定无符号byte数组根据指定下标获取长整型变量
	 * 
	 * @param 要获取的byte数组
	 * @param 获取byte数组的起始下标
	 * @return 获得的长整型变量
	 */
	public static long unsigned4BytesToInt(byte[] buf, int pos) {
		int firstByte = 0;
		int secondByte = 0;
		int thirdByte = 0;
		int fourthByte = 0;
		int index = pos;
		firstByte = (0x000000FF & ((int) buf[index]));
		secondByte = (0x000000FF & ((int) buf[index + 1]));
		thirdByte = (0x000000FF & ((int) buf[index + 2]));
		fourthByte = (0x000000FF & ((int) buf[index + 3]));
		index = index + 4;
		return ((long) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL;
	}

	/**
	 * 在指定短整型数组中根据指定下标取4字节的数据并转为长整型变量
	 * 
	 * @param 要获取的短整型数组
	 * @param 获取短整型数组的起始下标
	 * @return 获得的长整型标量
	 */
	public static long unsigned4ShortToInt(short[] sht, int pos) {
		int firstByte = 0;
		int secondByte = 0;
		int thirdByte = 0;
		int fourthByte = 0;
		int index = pos;
		firstByte = (0x000000FF & ((int) sht[index]));
		secondByte = (0x000000FF & ((int) sht[index + 1]));
		thirdByte = (0x000000FF & ((int) sht[index + 2]));
		fourthByte = (0x000000FF & ((int) sht[index + 3]));
		index = index + 4;
		return ((long) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL;
	}

	/**
	 * 获取byte数组的16进制字符串显示形式
	 * 
	 * @param 要获取的byte数组
	 * @return 获取到的16进制字符串显示形式
	 */
	public static String byte2hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1) {
				hs = hs + "0" + stmp + " ";
			} else {
				hs = hs + stmp + " ";
			}
		}
		return hs.toUpperCase();
	}

	/**
	 * 获取短整型数组的16进制字符串显示形式
	 * 
	 * @param 要获取的短整型数组
	 * @return 获取到的16进制字符串显示形式
	 */
	public static String short2hex(short[] shortList) {
		String result = "";
		String hex = "";
		for (int i = 0; i < shortList.length; i++) {
			hex = (Integer.toHexString(shortList[i] & 0XFF));
			if (hex.length() == 1) {
				result += "0" + hex + " ";
			} else {
				result += hex + " ";
			}
		}
		return result.toUpperCase();
	}

	/**
	 * 把指定短整型变量根据指定下标填充到指定的byte数组中
	 * 
	 * @param 填充到的byte数组
	 * @param 要填充的短整型变量
	 * @param 填充到byte数组的起始下标
	 */
	public static void putShortToBytes(byte b[], short s, int index) {
		b[index] = (byte) (s >> 8);
		b[index + 1] = (byte) (s >> 0);
	}

	/**
	 * 在指定byte数组中根据指定下标获取2字节的数据并转换为短整型标量
	 * 
	 * @param 要获取的byte数组
	 * @param 要获取byte数组的起始下标
	 * @return 获取到的短整型变量
	 */
	public static short getShortForBytes(byte[] b, int index) {
		return (short) (((b[index] << 8) | b[index + 1] & 0xff));
	}

	/**
	 * 把指定的整型变量根据指定下标填充到指定byte数组中
	 * 
	 * @param 填充到的byte数组
	 * @param 要填充的整型变量
	 * @param 填充到byte数组的起始下标
	 */
	public static void putIntToBytes(byte[] bb, int x, int index) {
		bb[index + 0] = (byte) (x >> 24);
		bb[index + 1] = (byte) (x >> 16);
		bb[index + 2] = (byte) (x >> 8);
		bb[index + 3] = (byte) (x >> 0);
	}

	/**
	 * int数组转byte数组
	 * 
	 * @param int数组
	 * @param byte数组,如果为空,
	 *            则new一个
	 * @return 转换好的byte数组
	 * @author 简航 创建日期 2009-08-06 23:46:12
	 */
	public static byte[] ints2bytes(int[] ints, byte[] bytes) {
		byte[] tempBytes = null;
		for (int i = 0, j = 0; i < bytes.length && j < bytes.length / 4; i = i + 4, j++) {
			tempBytes = intToByte(ints[j]);

			bytes[i] = tempBytes[0];
			bytes[i + 1] = tempBytes[1];
			bytes[i + 2] = tempBytes[2];
			bytes[i + 3] = tempBytes[3];
		}
		return tempBytes;
	}

	/**
	 * int变量转byte数组
	 * 
	 * @param 要转换的int变量
	 * @return 转换后的byte数组
	 * @author 简航 创建日期 2009-08-07 00:04:17
	 */
	public static byte[] int2bytes(int i) {
		byte[] bt = new byte[4];
		bt[0] = (byte) (0xff & i);
		bt[1] = (byte) ((0xff00 & i) >> 8);
		bt[2] = (byte) ((0xff0000 & i) >> 16);
		bt[3] = (byte) ((0xff000000 & i) >> 24);

		return bt;
	}

	/**
	 * 在指定byte数组中从指定下标开始获取4字节的byte数据并转换为整型变量
	 * 
	 * @param 要获取的byte数组
	 * @param 要获取的起始下标
	 * @return 获取到的整型变量
	 */
	public static int getIntForBytes(byte[] bb, int index) {
		return (int) ((((bb[index + 0] & 0xff) << 24)
				| ((bb[index + 1] & 0xff) << 16)
				| ((bb[index + 2] & 0xff) << 8) | ((bb[index + 3] & 0xff) << 0)));
	}

	/**
	 * 把指定的长整型变量根据指定下标填充到指定的byte数组中
	 * 
	 * @param 要填充的byte数组
	 * @param 长整型变量
	 * @param 填充到的byte数组下标
	 */
	public static void putLongToBytes(byte[] bb, long x, int index) {
		bb[index + 0] = (byte) (x >> 56);
		bb[index + 1] = (byte) (x >> 48);
		bb[index + 2] = (byte) (x >> 40);
		bb[index + 3] = (byte) (x >> 32);
		bb[index + 4] = (byte) (x >> 24);
		bb[index + 5] = (byte) (x >> 16);
		bb[index + 6] = (byte) (x >> 8);
		bb[index + 7] = (byte) (x >> 0);
	}

	/**
	 * 从byte数组中获取指定4个字节的数据并转为长整型变量
	 * 
	 * @param 目标byte数组
	 * @param 获取到的长整型变量
	 * @return
	 */
	public static long getLongForBytes(byte[] bb, int index) {
		return ((((long) bb[index + 0] & 0xff) << 56)
				| (((long) bb[index + 1] & 0xff) << 48)
				| (((long) bb[index + 2] & 0xff) << 40)
				| (((long) bb[index + 3] & 0xff) << 32)
				| (((long) bb[index + 4] & 0xff) << 24)
				| (((long) bb[index + 5] & 0xff) << 16)
				| (((long) bb[index + 6] & 0xff) << 8) | (((long) bb[index + 7] & 0xff) << 0));
	}

	/**
	 * byte数组转常整型
	 * 
	 * @param 要转换的byte数组
	 * @return 转换后的长整型变量
	 */
	public static long bytes2long(byte[] b) {
		long l = 0;
		l = b[0];
		l |= ((long) b[1] << 8);
		l |= ((long) b[2] << 16);
		l |= ((long) b[3] << 24);
		l |= ((long) b[4] << 32);
		l |= ((long) b[5] << 40);
		l |= ((long) b[6] << 48);
		l |= ((long) b[7] << 56);
		return l;
	}

	/**
	 * byte数组转长整型变量
	 * 
	 * @param 要转换的byte数组
	 * @return 转换后的长整型变量
	 */
	public static long bytes2long_2(byte[] bList) {
		long mask = 0xff;
		long temp = 0;
		long res = 0;
		for (int i = 0; i < 8; i++) {
			res <<= 8;
			temp = bList[i] & mask;
			res |= temp;
		}
		return res;
	}

	/**
	 * byte数组转长整型变量（重载1）
	 * 
	 * @param 要转换的byte数组
	 * @return 转换后的长整型变量
	 */
	public static long bytes2long_3(byte[] b) {
		long l = 0L;
		l = java.lang.Long.parseLong(new String(b));
		return l;
	}

	/**
	 * 长整型转byte数组
	 * 
	 * @param 要转换的常整型变量
	 * @return 转换后的byte数组
	 */
	public byte[] long2bytes(long l) {
		long temp = l;
		byte[] b = new byte[8];
		for (int i = b.length - 1; i > -1; i--) {
			b[i] = new Long(temp & 0xff).byteValue();
			temp = temp >> 8;
		}
		return b;
	}

	/**
	 * 长整型转byte数组
	 * 
	 * @param 要转换的长整型变量
	 * @return 转换后的byte数组
	 */
	public static byte[] long2bytes_2(long l) {
		byte[] b = new byte[8];
		for (int i = 0; i < 8; i++) {
			b[i] = (byte) (l >>> (56 - i * 8));
		}
		return b;
	}

	/**
	 * 长整型转byte数组
	 * 
	 * @param 要转换的长整型变量
	 * @return 转换后的byte数组
	 */
	public static byte[] long2bytes_3(long l) {
		byte[] b = new byte[8];
		b = java.lang.Long.toString(l).getBytes();
		return b;
	}

	/**
	 * 整型转byte数组
	 * 
	 * @param 要转换整型变量
	 * @param byte数组
	 * @param 数组从第几位开始接收
	 */
	public static void int2bytes(int n, byte buf[], int offset) {
		if (buf.length > offset)
			buf[offset] = (byte) (n >> 24);
		if (buf.length > offset + 1)
			buf[offset + 1] = (byte) (n >> 16);
		if (buf.length > offset + 2)
			buf[offset + 2] = (byte) (n >> 8);
		if (buf.length > offset + 3)
			buf[offset + 3] = (byte) n;
	}

	/**
	 * byte数组转int型
	 * 
	 * @param byte数组
	 * @return 转换后的整型变量
	 */
	public static int bytes2int(byte[] b) {
		int mask = 0xff;
		int temp = 0;
		int n = 0;
		for (int i = 0; i < b.length; i++) {
			n <<= 8;
			temp = b[i] & mask;
			n |= temp;
		}
		for (int i = b.length - 1; i < 4; i++) {
			b[i] = 0;
		}

		return n;
	}

	/**
	 * byte数组转整型（重载1）
	 * 
	 * @param 要转换的byte数组
	 * @param 从该数组的第几位开始转
	 * @return 转换后的整型变量
	 */
	public static int bytes2int(byte b[], int offset) {
		return b[offset + 3] & 0xff | (b[offset + 2] & 0xff) << 8
				| (b[offset + 1] & 0xff) << 16 | (b[offset] & 0xff) << 24;
	}

	public static int bytes2int_2(byte[] buf) {
		int firstByte = 0;
		int secondByte = 0;
		int thirdByte = 0;
		int fourthByte = 0;
		firstByte = (0x000000FF & ((int) buf[0]));
		secondByte = (0x000000FF & ((int) buf[1]));
		thirdByte = (0x000000FF & ((int) buf[2]));
		fourthByte = (0x000000FF & ((int) buf[3]));
		return ((int) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFF;
	}

	public static byte[] intToByte(int i) {
		byte[] bt = new byte[4];
		bt[0] = (byte) (0xff & i);
		bt[1] = (byte) ((0xff00 & i) >> 8);
		bt[2] = (byte) ((0xff0000 & i) >> 16);
		bt[3] = (byte) ((0xff000000 & i) >> 24);

		return bt;
	}

	public static int bytesToInt(byte[] bytes) {
		int num = bytes[0] & 0xFF;
		num |= ((bytes[1] << 8) & 0xFF00);
		num |= ((bytes[2] << 16) & 0xFF0000);
		num |= ((bytes[3] << 24) & 0xFF000000);

		return num;
	}
}
